// Created by carolsail

import { ENUM_UI } from '../GbzEnum';
import BaseLayer from '../layer/GbzBaselayer';
import PoolMgr from './GbzPoolManager';
import MainLayer from '../layer/GbzMainLayer';
import GbzInstance from '../GbzInstance';

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzUIManager extends cc.Component {

    private uiMap = new Map<ENUM_UI, BaseLayer>()

    protected onLoad(): void {
        GbzInstance.setUIMgr(this)
    }

    init() {
        for (let type in ENUM_UI) {
            const node: cc.Node = PoolMgr.ins.getNode(ENUM_UI[type], this.node)
            if (node && !this.uiMap.has(ENUM_UI[type])) {
                node.active = false
                node.addComponent(ENUM_UI[type])
                this.uiMap.set(ENUM_UI[type], node.getComponent(ENUM_UI[type]))
            }
        }
    }

    toggle(key: ENUM_UI, status: boolean = true, callback?: () => void) {
        if (this.uiMap.has(key)) {
            const layer = this.uiMap.get(key)
            status ? layer.show() : layer.hide()
            callback && callback()
        }
    }

    hasActive(key: ENUM_UI) {
        if (this.uiMap.has(key)) {
            return this.uiMap.get(key).node.active
        }
        return false
    }

    setMainCountDown(bool: boolean = true) {
        const layer: MainLayer = this.uiMap.get(ENUM_UI.LAYER_MAIN) as MainLayer
        if (bool) {
            layer?.startCountDown()
        } else {
            layer?.stopCountDown()
        }
    }

    setCurrentLevel() {
        const layer: MainLayer = this.uiMap.get(ENUM_UI.LAYER_MAIN) as MainLayer
        layer?.rendorCurrentLevel()
    }
}
